package com.rc.saas.tenant.controller.parking;

import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.base.BaseService;
import com.rc.saas.tenant.base.WebPageInfo;
import com.rc.saas.tenant.common.vo.TResponse;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.parking.ElecParkingGate;
import com.rc.saas.tenant.model.parking.ElecParkingGateExample;
import com.rc.saas.tenant.model.parking.TenantParkingSupplier;
import com.rc.saas.tenant.model.parking.TenantParkingSupplierExample;
import com.rc.saas.tenant.model.tenant.Tenant;
import com.rc.saas.tenant.search.tenant.TenantParkingSupplierSearch;
import com.rc.saas.tenant.service.elec.ElecStationService;
import com.rc.saas.tenant.service.parking.ElecParkingGateService;
import com.rc.saas.tenant.service.parking.TenantParkingSupplierService;
import com.rc.saas.tenant.service.tenant.TenantService;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Handler;

/**
 * @author wangzhenwei
 * @date 2020-06-11
 */
@Controller
@RequestMapping("/parking")
public class ParkingController {
    private static Logger logger = LoggerFactory.getLogger(ParkingController.class);

    @Autowired
    private TenantParkingSupplierService tenantParkingSupplierService;

    @Autowired
    private ElecStationService elecStationService;

    @Autowired
    private ElecParkingGateService elecParkingGateService;

    @Autowired
    private TenantService tenantService;


    /**
     * 跳转至 道闸供应商信息
     * @return
     */
    @RequestMapping(value = "/parkingSupplier", method = RequestMethod.GET)
    public String parkingSupplier(){
        return "/templates/parking/parkingSupplier";
    }

    /**
     * 道闸供应商列表
     * @param search
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listPageTenantParkingSupplier", method = RequestMethod.GET)
    public WebPageInfo listPageTenantParkingSupplier(TenantParkingSupplierSearch search){
        PageInfo<TenantParkingSupplier> pageInfo = tenantParkingSupplierService.listPageTenantParkingSupplier(search);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(),Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     *跳转至 添加道闸供应商页面
     */
    @RequestMapping(value = "/addParkingSupplier", method = RequestMethod.GET)
    public String addParkingSupplier(Model model, BaseSearch baseSearch){
        Tenant tenant = tenantService.selectByPrimaryKey(baseSearch.getTenantCode());
        model.addAttribute("tenant", tenant);
        return  "/templates/parking/addParkingSupplier";
    }


    /**
     *新增道闸供应商
     */
    @ResponseBody
    @RequestMapping(value = "/saveParkingSupplier", method = RequestMethod.POST)
    public TResponse saveParkingSupplier(TenantParkingSupplier tenantParkingSupplier){
        try {
            tenantParkingSupplierService.insertSelective(tenantParkingSupplier);
        }catch (Exception e){
            logger.error("新增道闸供应商失败", e);
            return TResponse.FAIL("新增道闸供应商失败");
        }
        return  TResponse.SUCCESS();

    }

    /**
     *跳转至 添加道闸页面
     */
    @RequestMapping(value = "/addParkingGate", method = RequestMethod.GET)
    public String addParkingGate(Model model, BaseSearch baseSearch,@RequestParam("parkingSupplierId") Long parkingSupplierId){
        Tenant tenant = tenantService.selectByPrimaryKey(baseSearch.getTenantCode());
        List<ElecStation> elecStationList = elecStationService.listElecStationNonconnectGate(baseSearch.getTenantCode());
        TenantParkingSupplier tenantParkingSupplier = tenantParkingSupplierService.selectByPrimaryKey(parkingSupplierId);
        model.addAttribute("tenant", tenant);
        model.addAttribute("elecStationList", elecStationList);
        model.addAttribute("tenantParkingSupplier",tenantParkingSupplier);
        return  "/templates/parking/addParkingGate";
    }


    /**
     * 新增道闸
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveParkingGate", method = RequestMethod.POST)
    public TResponse saveParkingGate(ElecParkingGate elecParkingGate){
        try {
            elecParkingGateService.insertSelective(elecParkingGate);
        }catch (Exception e){
            logger.error("新增道闸失败", e);
            return TResponse.FAIL("新增道闸失败");
        }
        return  TResponse.SUCCESS();
    }

    /**
     * 根据站点名查询站点(模糊查询)
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchStation",method = RequestMethod.GET)
    public List< ElecStation> searchStation(Model model,@RequestParam("tenantCode") String tenantCode,@RequestParam("stationName") String stationName){
        List<ElecStation> elecStationList = elecStationService.listElecStationByStationName(tenantCode, stationName);
        return elecStationList;
    }


    /**
     * 跳转至 道闸合作站点列表
     * @return
     */
    @RequestMapping(value = "/parkingGateStation", method = RequestMethod.GET)
    public String parkingGateStation(@RequestParam("tenantCode") String tenantCode,
                                     @RequestParam("parkingSupplierId") Long parkingSupplierId, Model model){
        model.addAttribute("tenantCode", tenantCode);
        model.addAttribute("parkingSupplierId", parkingSupplierId);
        return "/templates/parking/parkingGateStation";
    }

    /**
     * 道闸合作站点列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listPageParkingGateStation", method = RequestMethod.GET)
    public WebPageInfo listPageParkingGateStation(@RequestParam("offset") Integer offset,
                                                  @RequestParam("limit") Integer limit,
                                                  @RequestParam("tenantCode") String tenantCode,
                                                  @RequestParam("parkingSupplierId") Long parkingSupplierId,
                                                  @RequestParam("status") String status){
        PageInfo<ElecStation> pageInfo = elecStationService.listPageParkingGateStation(offset,limit,tenantCode, parkingSupplierId, status);
        WebPageInfo webPageInfo = new WebPageInfo(pageInfo.getList(), Long.valueOf(pageInfo.getTotal()).intValue());
        return webPageInfo;
    }

    /**
     * 下线道闸
     * @param elecStationId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/offlineParkingGateStation", method = RequestMethod.POST)
    public TResponse offlineParkingGateStation(Long elecStationId){
        ElecParkingGateExample example = new ElecParkingGateExample();
        example.createCriteria().andElecStationIdEqualTo(elecStationId);
        List<ElecParkingGate> elecParkingGateList = elecParkingGateService.selectByExample(example);
        for (ElecParkingGate elecParkingGate:elecParkingGateList){
            elecParkingGate.setStatus("1");
            elecParkingGateService.updateByPrimaryKey(elecParkingGate);
        }
        return TResponse.SUCCESS();
    }
}
